package main

import (
	"day7pro/goweb/base5/gee"
	"net/http"
)

// step5 路由管理用前缀树

func main() {
	r := gee.New()
	r.GET("/", func(c *gee.Context) {
		c.HTML(http.StatusOK, "<h1>Hello Gee</h1>")
	})

	r.GET("/hello", func(c *gee.Context) {
		// expect /hello?name=xxx
		c.String(http.StatusOK, "hello %s, you are at %s\n", c.Query("name"), c.Path)
	})

	// 新增动态路由的请求
	r.GET("/hello/:name", func(c *gee.Context) {
		// expect /hello/:name
		c.String(http.StatusOK, "hello %s, you are at %s\n", c.Param("name"), c.Path)
	})

	// 新增对*匹配的支持
	r.GET("/assets/*filepath", func(c *gee.Context) {
		// $ curl "http://localhost:9999/assets/css/geektutu.css"
		// {"filepath":"css/geektutu.css"}
		c.JSON(http.StatusOK, gee.H{"filePath": c.Param("filePath")})
	})

	r.POST("/login", func(c *gee.Context) {
		c.JSON(http.StatusOK, gee.H{
			"username": c.PostForm("username"),
			"password": c.PostForm("password"),
		})
	})
	r.Run(":8000")
}
